package cn.js.sangu.unitStore.controller;

import cn.hutool.core.util.StrUtil;
import cn.js.sangu.unitStore.common.LogType;
import cn.js.sangu.unitStore.common.MyAuthAccess;
import cn.js.sangu.unitStore.common.MyLogsAOP;
import cn.js.sangu.unitStore.common.R;
import cn.js.sangu.unitStore.entity.Audit;
import cn.js.sangu.unitStore.service.AuditService;
import cn.js.sangu.unitStore.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 说明：审批记录接口
 * 作者：linshuo
 * 日期：2024/8/7 8:40
 */
//@CrossOrigin //已有全局跨域设置CorsConfig
@RequestMapping("audit")
@RestController
public class AuditController {

    static final String OPERATION_AUDIT = "审批记录";

    @Autowired
    AuditService auditService;

    @Autowired
    UserService userService;

    @MyLogsAOP(operation = OPERATION_AUDIT,operaType = LogType.LOOKUP)
    @GetMapping("findByPage")
    public R findByPage(@RequestParam Integer pageNum,
                        @RequestParam Integer pageSize,
                        @RequestParam String auditName,
                        @RequestParam String creator,
                        @RequestParam String recipient){
        QueryWrapper<Audit> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(auditName),"audit_name",auditName);
        wrapper.eq(StrUtil.isNotBlank(creator),"creator",creator);
        wrapper.like(StrUtil.isNotBlank(recipient),"recipient",recipient);
        wrapper.orderByDesc("id");
        //构建page对象
        Page<Audit> param = new Page<>(pageNum, pageSize);
        Page<Audit> res = auditService.page(param, wrapper);
        return R.ok(res.setRecords(suppleName(res.getRecords())));
    }

    @MyLogsAOP(operation = OPERATION_AUDIT,operaType = LogType.ADD)
    @RequestMapping("save")
    public R save(@RequestBody Audit dto){
        auditService.saveInfo(dto);
        return R.ok("保存成功");
    }

    @MyLogsAOP(operation = OPERATION_AUDIT,operaType = LogType.DELETE)
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable Integer id){
        auditService.removeById(id);
        return R.ok("删除成功");
    }

    @MyLogsAOP(operation = OPERATION_AUDIT,operaType = LogType.BATCH_DELETE)
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids){
        auditService.removeBatchByIds(ids);
        return R.ok("批量删除成功");
    }

//    @MyLogsAOP(operation = OPERATION_AUDITUMENT,operaType = LogType.LOOKUP)
    @MyAuthAccess
    @GetMapping("findByBindId/{bindId}")
    public R findById(@PathVariable String bindId){
        QueryWrapper<Audit> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(bindId),"bind_id",bindId);
        List<Audit> list = auditService.list(wrapper);

//        for ()

        //建议保存时保存用户名，这里就不用频繁查询了。
//        User user = userService.getById(audit.getCreator());
//        if (null != user){
//            audit.setCreatorName(user.getName());
//        }
//        return R.ok(audit);
        return null;
    }

//    @MyLogsAOP(operation = OPERATION_AUDITUMENT,operaType = LogType.LOOKUP)
    @GetMapping("findByRecent")
    public R findByRecent(){
        LambdaQueryWrapper<Audit> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(Audit::getCreatTime).last("LIMIT 10");
        return R.ok(suppleName(auditService.list(wrapper)));
    }

    /**
     * 补充创建人+修改人+接收人 名称
     * @param list
     * @return
     */
    private List<Audit> suppleName(List<Audit> list ){
//        for (Audit audit : list) {
//            //创建人
//            User author = userService.getById(audit.getCreator());
//            if (null != author) audit.setCreatorName(author.getName());
//            //修改人
//            User modifier = userService.getById(audit.getModifier());
//            if (null != modifier) audit.setModifierName(modifier.getName());
//            //接收人
//            User recipient = userService.getById(audit.getRecipient());
//            if (null != recipient) audit.setRecipientName(recipient.getName());
//            //接收人
//            User auditor = userService.getById(audit.getAuditor());
//            if (null != auditor) audit.setAuditorName(auditor.getName());
//        }
//        return list;
        return null;
    }

}
